package com.bjdd.jdy.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.bjdd.jdy.App;
import com.bjdd.jdy.R;
import com.bjdd.jdy.baidu.LocationUtils;
import com.bjdd.jdy.baidu.MyService;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.base.RxManager;
import com.bjdd.jdy.entity.User;
import com.bjdd.jdy.ui.Mine.MineFragment;
import com.bjdd.jdy.ui.helpBuy.HelpBuyFragment;
import com.bjdd.jdy.ui.home.HomeFragment;
import com.bjdd.jdy.ui.news.NewsFragment;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.T;
import com.google.zxing.activity.CaptureActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by yukai on 2016/11/13.
 */

public class MainActivity extends BaseActivity<MainPresenter, MainModel> implements MainContract.View {


    @Bind(R.id.fl_tabcontent)
    FrameLayout flTabcontent;
    @Bind(android.R.id.tabcontent)
    FrameLayout tabcontent;
    @Bind(android.R.id.tabhost)
    FragmentTabHost tabhost;
    private FragmentManager fm;
    private HomeFragment homeFragment;
    private RxManager rxManager = new RxManager();;
    private LayoutInflater mLayoutInflater;
    private Class mFragmentArray[] = {HomeFragment.class,HelpBuyFragment.class,NewsFragment.class,MineFragment.class};
    private int mImageArray[] = {R.drawable.tab_menu_home,R.drawable.tab_menu_help_buy,R.drawable.tab_menu_news,R.drawable.tab_menu_mine};
    private String mTextArray[] = {"帮送", "帮买", "消息", "我的"};

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    public void initView() {

        mLayoutInflater = LayoutInflater.from(this);
        tabhost.setup(this,getSupportFragmentManager(),R.id.fl_tabcontent);
        //获得fragment的个数
        int count = mFragmentArray.length;
        for (int i=0;i < count;i ++){
            //给每个tab按钮设置图标，文字和内容
            TabHost.TabSpec tabSpec = tabhost.newTabSpec(mTextArray[i]).setIndicator(getTabItemView(i));
            tabhost.addTab(tabSpec,mFragmentArray[i],null);
            tabhost.getTabWidget().setDividerDrawable(null);
            //mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.white);
        }
    }

    @Override
    public void initUser(User user) {
//
    }

    @Override
    public void finishActivity() {
        finish();
    }






    /**
     *
     * 给每个Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.tab_img);
        imageView.setImageResource(mImageArray[index]);
        TextView textView = (TextView) view.findViewById(R.id.tab_tv);
        textView.setText(mTextArray[index]);
        return view;
    }

    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                T.show("再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                App.getApp().exit();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
